package com.algorithm.string;


import java.util.ArrayList;

/**
 * 字符串相关经典算法
 * */
public class Classic {

    public static void main(String args[]){

        //String content = "pwwkew";
        //System.out.println("最大连续次数："+findMostLianxuSize(content));
        //System.out.println("最大连续："+findMostLianxuSizeCommon(content));

        //拆分汉字和字符数字
        System.out.print(spitWord("sop1娃哈A哈123测试abc"));
    }




    /*查询字符串最大连续次数*/
    private static int findMostLianxuSize(String content){

        Integer [] hash = new Integer[500];
        int max = 0;
        int i = 0,j=0;

        while (i<hash.length&&j<hash.length){
            if(hash[content.charAt(j)]==0){
                hash[content.charAt(j)]=1;
                j++;
                max = (j-i)>max?(j-i):max;
            }else{
                hash[content.charAt(i)]=0;
                i++;
            }
        }

        return max;

    }


    /*查询字符串最大连续次数*/
    private static int findMostLianxuSizeCommon(String content) {

        //上一次最大连续次数
        int lastMax = 1;
        //当前连续最大的次数
        int maxTemp = 1;
        //保存上一个数值记录
        int currentNumTemp = -1;


        char[] mContentArray = content.toCharArray();
        for (int i = 0; i < mContentArray.length; i++) {

            int num = mContentArray[i];
            System.out.print(num+"-");

            if (num - currentNumTemp == 1) {
                maxTemp++;
            } else {
                if (maxTemp >= lastMax) {
                    lastMax = maxTemp;
                }
                maxTemp = 1;
            }
            currentNumTemp = num;
        }

        return lastMax;
    }

    static int isCnorEn(char c){
        //中文字符
        if(c >= 0x0391 && c <= 0xFFE5)
            return 1;
        //英文字符
        if(c>=0x0000 && c<=0x00FF){
            return 2;
        }
        return 3;
    }

    public static ArrayList<String> spitWord(String keyWord){
        ArrayList<String> mList = new ArrayList<>();

        StringBuffer mBuffer = null;
        for(int i = 0;i<keyWord.length();i++){
            char name = keyWord.charAt(i);
            if(isCnorEn(name)==1){
                if(mBuffer!=null){
                    mList.add(mBuffer.toString());
                    mBuffer = null;
                }else{
                    mBuffer = null;
                }
                mList.add(String.valueOf(name));
            }else{
                if(mBuffer==null){
                    mBuffer = new StringBuffer();
                }
                mBuffer.append(name);
            }
        }
        if(mBuffer!=null){
            mList.add(mBuffer.toString());
        }

        return mList;
    }
}

